/*
 * Copyright 2017. nekocode (nekocode.cn@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nekocode.dividerdrawable;

import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author nekocode (nekocode.cn@gmail.com)
 */
public class DividerUtils {
    /**
     * 上下文
     */
    public static Context context;
    /**
     * 控件
     */
    public static Component components;
    /**
     * 集合
     */
    public static HashMap<Integer, List<DividerBean>> hashMap = new HashMap<>();

    public static void setContext(Context contexts) {
        context = contexts;
    }

    /**
     * 给控件绘画
     *
     * @param component       控件
     * @param dividerDrawable 控件
     */
    public static void addDividersTo(Component component, DividerDrawable dividerDrawable) {
        components = component;
        invalidateSelf(dividerDrawable);

        List<DividerBean> list = hashMap.get(component.getId());

        if (list != null && list.size() > 0) {
            DividerBean bean = new DividerBean();
            bean.setDividerDrawable(dividerDrawable);
            bean.setComponent(component);
            list.add(bean);
            hashMap.put(component.getId(), list);
        } else {
            List<DividerBean> list1 = new ArrayList<>();
            DividerBean bean1 = new DividerBean();
            bean1.setDividerDrawable(dividerDrawable);
            bean1.setComponent(component);
            list1.add(bean1);
            hashMap.put(component.getId(), list1);
        }
    }

    /**
     * 刷新布局
     *
     * @param dividerDrawable 控件
     */
    public static void invalidateSelf(DividerDrawable dividerDrawable) {
        components.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                List<DividerBean> list = hashMap.get(component.getId());
                int width = component.getWidth();
                int height = component.getHeight();

                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        DividerBean bean = list.get(i);
                        DividerDrawable dividerDrawable = bean.getDividerDrawable();
                        dividerDrawable.setComponent(component);
                        dividerDrawable.setWidthAndHight(width, height);
                        dividerDrawable.drawToCanvas(canvas);
                    }
                    dividerDrawable.setComponent(component);
                    dividerDrawable.setWidthAndHight(width, height);
                    dividerDrawable.drawToCanvas(canvas);
                } else {
                    dividerDrawable.setComponent(component);
                    dividerDrawable.setWidthAndHight(width, height);
                    dividerDrawable.drawToCanvas(canvas);
                }
            }
        }, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
    }

    /**
     * 多个ShapeElement合并成一个
     *
     * @param shapeElement    控件
     * @param dividerDrawable 控件
     * @return 控件
     */
    public static ShapeElement addDividersTos(ShapeElement shapeElement, DividerDrawable... dividerDrawable) {
        return new CombinedDrawable(shapeElement, dividerDrawable);
    }

    /**
     * 清除绘画
     *
     * @param component 控件
     */
    public static void clearDividersWith(Component component) {
        component.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                List<DividerBean> list = hashMap.get(component.getId());
                list.clear();
                ShapeElement shapeElement = (ShapeElement) component.getBackgroundElement();

                shapeElement.drawToCanvas(canvas);
            }
        }, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
    }

    /**
     * 清除绘画
     *
     * @param shapeElement 控件
     * @return 控件
     */
    public static ShapeElement clearDividersWith(ShapeElement shapeElement) {
        if (shapeElement instanceof CombinedDrawable) {
            return clearDividersWith(((CombinedDrawable) shapeElement).origin);
        } else {
            return shapeElement;
        }
    }

    /**
     * dp 转换 px
     *
     * @param context  上下文
     * @param dipValue dp
     * @return px
     */
    public static float dp2px(Context context, float dipValue) {
        final float scale = context.getResourceManager().getDeviceCapability().screenDensity / 160 * dipValue;
        return scale;
    }

    private static void setBackground(Component component, ShapeElement shapeElement) {
        component.addDrawTask(((component1, canvas) -> shapeElement.drawToCanvas(canvas)));
    }

    /**
     * 多个ShapeElement合并类
     */
    public static class CombinedDrawable extends ShapeElement {
        private ShapeElement origin;

        CombinedDrawable(ShapeElement origin, DividerDrawable[] divider) {
            combine(origin, divider);
            this.origin = origin;
        }

        static ShapeElement[] combine(ShapeElement origin, DividerDrawable[] dividers) {
            final int hasOrigin = origin != null ? 1 : 0;
            final ShapeElement[] elements = new ShapeElement[dividers.length + hasOrigin];
            if (hasOrigin > 0) {
                elements[0] = origin;
            }

            System.arraycopy(dividers, 0, elements, hasOrigin, dividers.length);

            return elements;
        }
    }
}
